﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.IO;
/*
Copyright 2011 Mirosław M. Mączka

This file is part of MobiLang.

    MobiLang is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace MobiLang
{
    public partial class Form1 : Form
    {
        private struct ebook
        {
            public string ShortName;
            public string PathName;
            public ebook(string ShortName, string PathName)
            {
                this.ShortName = ShortName;
                this.PathName = PathName;
            }
            public ebook(string PathName)
            {
                this.ShortName = Path.GetFileNameWithoutExtension(PathName);
                this.PathName = PathName;
            }
        }
        private MobiHandler mh = new MobiHandler();
        //
        private int idxFiles = 0;
        private List<ebook> files = new List<ebook>();
        //
        public Form1()
        {
            //
            InitializeComponent();
            //
            this.Text = getVersionInfo();
            //
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ClearForm();
            //
            listBox_Locale.DataSource = LanguageCodes.GetLangs();
            listBox_Locale.DisplayMember = "Name";
            //
            listBox_InputLanguage.DataSource = LanguageCodes.GetLangs();
            listBox_InputLanguage.DisplayMember = "Name";
            //
            listBox_OutputLanguage.DataSource = LanguageCodes.GetLangs();
            listBox_OutputLanguage.DisplayMember = "Name";
            //
            ShowNewFiles();
            //
        }

        private void button_Left_Click(object sender, EventArgs e)
        {
            ShowFileLeft();
        }

        private void button_Right_Click(object sender, EventArgs e)
        {
            ShowFileRight();
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            //
            button_Save.Enabled = false;
            //
            if (mh.FileOK)
            {
                mh.SetLangLocal(((LanguageCodes.Lang)listBox_Locale.SelectedItem).Lcid);
                mh.SetLangInputLanguage(((LanguageCodes.Lang)listBox_InputLanguage.SelectedItem).Lcid);
                mh.SetLangOutputLanguage(((LanguageCodes.Lang)listBox_OutputLanguage.SelectedItem).Lcid);
            }
            //
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //
                files = new List<ebook>();
                //
                e.Effect = DragDropEffects.Copy;
                //
                string targetDir = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
                //
                if (!Directory.Exists(targetDir))//zapewne to plik
                {
                    files.Add(new ebook(targetDir));
                }
                if (Directory.Exists(targetDir))
                {
                    foreach (string s in Directory.EnumerateFiles(targetDir, "*.mobi", SearchOption.AllDirectories))
                    {
                        files.Add(new ebook(s));
                    }
                    foreach (string s in Directory.EnumerateFiles(targetDir, "*.prc", SearchOption.AllDirectories))
                    {
                        files.Add(new ebook(s));
                    }
                    files.Sort((a, b) => (string.Compare(a.ShortName, b.ShortName, true)));
                }
                //
                if (files.Count > 0)
                {
                    ShowNewFiles();
                }
                //
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
            //
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void Form1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void listBox_Locale_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!listBox_Locale.Enabled) return;
            if (null == listBox_Locale.SelectedItem) return;
            button_Save.Enabled = true;
            textBox_Locale.Text = ((LanguageCodes.Lang)listBox_Locale.SelectedItem).Lcid.ToString();
        }

        private void listBox_InputLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!listBox_InputLanguage.Enabled) return;
            if (null == listBox_InputLanguage.SelectedItem) return;
            button_Save.Enabled = true;
            textBox_InputLanguage.Text = ((LanguageCodes.Lang)listBox_InputLanguage.SelectedItem).Lcid.ToString();
        }

        private void listBox_OutputLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!listBox_OutputLanguage.Enabled) return;
            if (null == listBox_OutputLanguage.SelectedItem) return;
            button_Save.Enabled = true;
            textBox_OutputLanguage.Text = ((LanguageCodes.Lang)listBox_OutputLanguage.SelectedItem).Lcid.ToString();
        }

        private void button_Save_EnabledChanged(object sender, EventArgs e)
        {
            if (button_Save.Enabled)
            {
                button_Save.BackColor = Color.Coral;
            }
            else
            {
                button_Save.BackColor = Color.FromKnownColor(KnownColor.ButtonShadow);
            }
        }

    }
}
